package com.loe.ktui.util

import android.graphics.drawable.GradientDrawable
import com.loe.ktui.dist
import com.loe.ktui.distFloat

class UiShape
{
    /** 类型：rect  line  ring  oval */
    var type: String? = null

    /** 圆角*/
    var corner: Any? = null
    var corners: Array<Any>? = null

    /** 颜色 */
    var color: Int? = null
    var colors: Array<Int>? = null

    /** 边框*/
    var strokeWidth: Any? = "1"
    var strokeColor: Int? = null
    var strokeDash: Any? = null
    var strokeGap: Any? = null

    /** 中心 */
    var centerX: Double? = null
    var centerY: Double? = null

    /** 渐变：linear  radial  sweep */
    var gradient: String? = null
    var gradientRadius: Any? = null
    var gradientAngle: Int? = null

    /** 大小 */
    var size: Any? = null
    var width: Any? = null
    var height: Any? = null

    /** 创建Drawable */
    fun createDrawable(): GradientDrawable
    {
        val drawable = GradientDrawable()

        drawable.shape = when (type)
        {
            "line" -> GradientDrawable.LINE
            "ring" -> GradientDrawable.RING
            "oval" -> GradientDrawable.OVAL
            else -> GradientDrawable.RECTANGLE
        }

        corner?.let { drawable.cornerRadius = it.distFloat }
        corners?.let {
            val cs = FloatArray(8) { 0f }
            it[0].distFloat.let { cs[0] = it; cs[1] = it }
            it[1].distFloat.let { cs[2] = it; cs[3] = it }
            it[2].distFloat.let { cs[4] = it; cs[5] = it }
            it[3].distFloat.let { cs[6] = it; cs[7] = it }
            drawable.cornerRadii = cs
        }

        color?.let { drawable.setColor(it) }
        colors?.let { drawable.colors = it.toIntArray() }

        strokeColor?.let {
            if(strokeDash != null)
            {
                val dash = strokeDash.distFloat
                val gap = if (strokeGap != null) strokeGap.distFloat else dash
                drawable.setStroke(strokeWidth.dist, it, dash, gap)
            }else
            {
                drawable.setStroke(strokeWidth.dist, it)
            }
        }

        if (centerX != null && centerY != null)
        {
            drawable.setGradientCenter(centerX!!.toFloat(), centerY!!.toFloat())
        }

        gradient?.let {
            drawable.gradientType = when (it)
            {
                "radial" ->
                {
                    gradientRadius?.let { drawable.gradientRadius = it.distFloat }
                    GradientDrawable.RADIAL_GRADIENT
                }
                "sweep" -> GradientDrawable.SWEEP_GRADIENT
                else -> GradientDrawable.LINEAR_GRADIENT
            }
        }

        gradientAngle?.let {
            val angle = ((it + 360) % 360)
            drawable.orientation = when(angle / 45)
            {
                7 -> GradientDrawable.Orientation.TL_BR
                6 -> GradientDrawable.Orientation.TOP_BOTTOM
                5 -> GradientDrawable.Orientation.TR_BL
                4 -> GradientDrawable.Orientation.RIGHT_LEFT
                3 -> GradientDrawable.Orientation.BR_TL
                2 -> GradientDrawable.Orientation.BOTTOM_TOP
                1 -> GradientDrawable.Orientation.BL_TR
                else -> GradientDrawable.Orientation.LEFT_RIGHT
            }
        }

        if(size != null)
        {
            val d = size.dist
            drawable.setSize(d, d)
        }else
        {
            if (width != null || height != null)
            {
                drawable.setSize((width ?: 0).dist, (height ?: 0).dist)
            }
        }

        return drawable
    }
}

fun ui_Shape(block: (UiShape.() -> Unit)? = null): UiShape
{
    val shape = UiShape()
    block?.let { shape.it() }
    return shape
}